package com.xd;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;

import java.util.Set;
import java.util.concurrent.TimeUnit;

//@SpringBootTest
public class test {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testRedisTemplate(){
        System.out.println(redisTemplate);
        redisTemplate.opsForValue();
        redisTemplate.opsForHash();
        redisTemplate.opsForList();
        redisTemplate.opsForSet();
        redisTemplate.opsForSet();
    }
    @Test
    public void test2(){
        redisTemplate.opsForValue().set("name","小明");
        String  name = (String)redisTemplate.opsForValue().get("name");
        System.out.println(name);
        redisTemplate.opsForValue().set("code","1234",3, TimeUnit.MINUTES);
        redisTemplate.opsForValue().setIfAbsent("lock","1");
        redisTemplate.opsForValue().setIfAbsent("lock","2");

    }
//    Hash类型的指令
    public void testHash(){

        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("100","name","tom");
        hashOperations.put("100","age","12");
        String name = (String) hashOperations.get("100", "name");
        System.out.println(name);
        Set keys = hashOperations.keys("100");
        System.out.println(keys);
        hashOperations.delete("100","age");

    }
//    操作列表类型的数据
    @Test
    public void testList(){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("mylist","a","b","c");
        listOperations.leftPush("mylist","d");
        listOperations.rightPop("mylist");
        Long mylist = listOperations.size("mylist");
    }

    @Test
    public void testSet(){
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("set1","a","b","c","d");
        setOperations.add("set2","a","b","x","y");
        Set set1 = setOperations.members("set1");
        System.out.println(set1);
        Long set2 = setOperations.size("set1");
        System.out.println(set2);
//        集合之间取并集
        Set union = setOperations.union("set1", "set2");
        Set intersect = setOperations.intersect("set1", "set2");
        System.out.println(intersect);
        System.out.println(union);
        setOperations.remove("set1","a","b");

    }
//    有序集合
    @Test
    public void test4(){
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        zSetOperations.add("zset1","a",10);
        zSetOperations.add("zset1","c",10);
        Set set1 = zSetOperations.range("zset1", 0, -1);
        System.out.println(set1);
        zSetOperations.incrementScore("zset","c",10);
        zSetOperations.remove("zset1","a","b");
    }

}
